import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';

@Injectable()
export class tryParseIntPipe implements PipeTransform<string, object> {
  transform(value: string, metadata: ArgumentMetadata): object {
    if(!value) {
      return {}
    }
    console.log(value)
    if (Object.getOwnPropertyNames(value).length == 0) {
      return value as Object
    }
    let query = JSON.parse(value)

    for (const key in query) {
      if (['skip', 'limit', 'page'].includes(key)) {
        try {
          if (isNaN(parseInt(query[key]))) {
            throw new BadRequestException('转换失败')
          } else {
            console.log(query[key])
            query[key] = parseInt(query[key])
          }
        } catch (error) {
          console.log(`${query[key]} 不能转化为数字`)
        }
      }
    }
    return query;
  }
}
